/*
 *  Queen Mary University of London MOM System
 *  
 *  Copyright Queen Mary University of London
 *  
 *  Authors:
 *  Bob Chew (bob.chew@elec.qmul.ac.uk)
 *  Beatriz Viñal Murciano (beavimu@gmail.com)
 *  John Bigham (john.bigham@elec.qmul.ac.uk)
 *  
 *  
 *  Portions of this software uses Apache Qpid
 *    http://qpid.apache.org
 *  
 *  Apache Qpid license:
 * --------------------------------------------------------------
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * -------------------------------------------------------------
 * 
 */
package overlayManager;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.apache.qpid.client.message.JMSTextMessage;

/**
 * Listener to messages from clients.
 */
public class RequestMessageListener implements MessageListener {

    /** Overlay manager to which the listener belongs. */
    private OverlayManager om;

    /**
     * Index of the first topic in the array resulting from splitting the text
     * of the messages asking for a broker assignment around matches of ":".
     */
    private static final int TOPIC1_INDEX = 3;

    /**
     * Constructor.
     *
     * @param om  overlay manager to which the listener belongs
     */
    public RequestMessageListener(final OverlayManager om) {
        this.om = om;
    }

    /**
     * Invoked when a message from a client arrives.
     *
     * Primary and mirror brokers for each topic are assigned to publishers and
     * subscribers and a message is sent to the client.
     *
     * @param msg  received message
     */
    @Override
    public void onMessage(final Message msg) {

        try {
            //get client id from the message
            JMSTextMessage message = (JMSTextMessage) msg;
            String text = message.getText();
            String[] parts = text.split(":");

            //ID of the client
            String clientId = parts[2];
            //topics the publisher is publishing to or the subscriber
            //is subscribed to
            String [] topics = new String[parts.length - TOPIC1_INDEX];
            for (int i = TOPIC1_INDEX; i < parts.length; i++) {
                topics[i - TOPIC1_INDEX] = parts[i];
            }

            //if the user is a publisher
            if (parts[1].equals("publisher")) {
                //assign primary and mirror broker for each topic
                om.assignBrokersToPub(clientId, topics);
            } else { //if the user is a subscriber
                //assign primary and mirror broker for each topic
                om.assignBrokersToSub(clientId, topics);
            }

        } catch (JMSException e) {
            e.printStackTrace();
        }

    }

}
